package patrol.mobile.com.mobilepatrol.activity.office;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

public class OtherBusinessSearchAllActivity extends BaseActivity implements CallBack<DateBean> {

    @BindView(R.id.mySpinner01)
    MySpinner mySpinner01;
    @BindView(R.id.mySpinner02)
    MySpinner mySpinner02;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.btn_sure)
    Button btnSure;
    @BindView(R.id.et_search)
    EditText etSearch;
    private int clickFlag;//点击选择时间的标志 1是开始时间 2是结束时间
    private String currentStartTime = "";
    private String currentEndTime = "";
    private OrgListBean orgListBean;
    private String currentOrgId;
    private String currentPosition = "0";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_other_business_search_all;
    }

    @Override
    public void initView() {
        super.initView();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CommonUtil.exitActivityAndBackAnim(OtherBusinessSearchAllActivity.this, true);
                    }
                });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("机构选择");
    }

    @Override
    public void setListener() {
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickFlag = 1;
                DatePickerUtil.getInstance(OtherBusinessSearchAllActivity.this).setCallBack(OtherBusinessSearchAllActivity.this);
            }
        });

        tvEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickFlag = 2;
                DatePickerUtil.getInstance(OtherBusinessSearchAllActivity.this).setCallBack(OtherBusinessSearchAllActivity.this);
            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                String searchText = etSearch.getText().toString().trim();
                bundle.putString("currentPosition", currentPosition);
                bundle.putString("currentOrgId", currentOrgId);
                bundle.putString("currentStartTime", currentStartTime);
                bundle.putString("currentEndTime", currentEndTime);
                bundle.putString("searchText", searchText);
                intent.putExtra("result", bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        getOrg();
        final List<String> list = new ArrayList<>();
        list.add("未完成");
        list.add("已完成");
        mySpinner02.setmDatas(list, new MySpinner.CallBack() {
            @Override
            public void onSelect(int postion) {
                if (postion == 0) {
                    currentPosition = "0";
                } else {
                    currentPosition = "1";
                }
            }
        });
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
//        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                currentOrgId = orgListBean.getList().get(0).getId();
                mySpinner01.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        currentOrgId = orgListBean.getList().get(postion).getId();
                    }
                });
            }
        });
    }

    @Override
    public void onBack(DateBean dateBean) {
        if (clickFlag == 1) {
            currentStartTime = dateBean.getmDate();
            tvStart.setText(dateBean.getmDate());
        } else {
            currentEndTime = dateBean.getmDate();
            tvEnd.setText(dateBean.getmDate());
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
